<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Centre page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="common.js"></script>
    <script type="text/javascript">

        function saveRespHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            switch (result_text.toLowerCase()) {
                case "added":
                    alert("Centre added!")
                    break;
                case "updated":
                    alert("Centre updated");
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the save response");
                    break;
            }
        }

        function queryRespHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            switch (result_text.toLowerCase()) {
                case "deleted":
                    alert('Deleted centre');
                    resetForm();
                    break;
                case "success":
                    //alert('Successful query');
                    try {
                        centre_name=xmlDoc.getElementsByTagName("centre_name");
                        document.getElementById("centre_name").value=centre_name[0].firstChild.nodeValue;
                        centre_code=xmlDoc.getElementsByTagName("centre_code");
                        document.getElementById("centre_code").value=centre_code[0].firstChild.nodeValue;
                        aplh_code=xmlDoc.getElementsByTagName("aplh_code");
                        document.getElementById("aplh_code").value=aplh_code[0].firstChild.nodeValue;
                    } catch (er) {
                        alert('Invalid centre code');
                    }
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the query response");
                    break;
            }
        }

        function dispTableHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            table_text = "<button onclick='displayTable()'>Display the centre table</button>";
            table_text = table_text + "<table border='1'>"+
                      "<tr><th>ID</th>"+
                          "<th>Centre Name</th>"+
                          "<th>Centre Code</th>"+
                          "<th>APLH. Code</th></tr>";

            switch (result_text.toLowerCase()) {
                case "success":
                    //alert('Successful query');
                    centres=xmlDoc.getElementsByTagName("centre");
                    for (i=0;i<centres.length;i++) {
                        table_text = table_text + "<tr>";
                        try {
                            table_text = table_text + insertField(centres[i], "centre_id");
                            table_text = table_text + insertField(centres[i], "centre_name");
                            table_text = table_text + insertField(centres[i], "centre_code");
                            table_text = table_text + insertField(centres[i], "aplh_code");
                        } catch (er) {
                            alert('Invalid centre code');
                        }
                        table_text = table_text + "</tr>";
                    }
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the query response");
                    break;
            }

            table_text = table_text + "</table>";
            document.getElementById("centre_table").innerHTML=table_text;
        }

        function validateForm() {
            var return_value = true;
            var centre_name=document.getElementById("centre_name").value;
            var centre_code=document.getElementById("centre_code").value;
            var aplh_code=document.getElementById("aplh_code").value;

            if (!centre_name) {
                document.getElementById("centre_name_hint").innerHTML =
                    "<p style='color:red'><b>Name cannot be empty </b></p>";
                return_value = false;
            } else {
                document.getElementById("centre_name_hint").innerHTML = " ";
            }

            if (!centre_code) {
                document.getElementById("centre_code_hint").innerHTML =
                    "<p style='color:red'><b>Code cannot be empty </b></p>";
                return_value = false;
            } else {
                if (isNaN(centre_code)) {
                    document.getElementById("centre_code_hint").innerHTML =
                        "<p style='color:red'><b>Code must be a number </b></p>";
                    return_value = false;
                } else {
                    document.getElementById("centre_code_hint").innerHTML = " ";
                }
            }

            if (!aplh_code) {
                document.getElementById("aplh_code_hint").innerHTML =
                    "<p style='color:red'><b>APLH. Code cannot be empty </b></p>";
                return_value = false;
            } else {
                document.getElementById("aplh_code_hint").innerHTML = " ";
            }

            return return_value;

        }
        
        function saveForm() {
            if (!validateForm()) {
                alert('Correct errors in the form before trying again!');
                return;
            }

            var centre_name=document.getElementById("centre_name").value;
            var centre_code=document.getElementById("centre_code").value;
            var aplh_code=document.getElementById("aplh_code").value;

            srvr_request="savecentre.php?centre_name="+centre_name+
                            "&centre_code="+centre_code+
                            "&aplh_code="+aplh_code;
                        
            ajaxCall(srvr_request,saveRespHdlr);
        }

        function resetForm() {
            document.getElementById("centre_name").value = "";
            document.getElementById("centre_name_hint").innerHTML = "";
            document.getElementById("centre_code").value = "";
            document.getElementById("centre_code_hint").innerHTML = "";
            document.getElementById("aplh_code").value = "";
            document.getElementById("aplh_code_hint").innerHTML = "";
        }

        function queryForm() {
            var centre_code=document.getElementById("centre_code").value;
            srvr_request="querycentre.php?centre_code="+centre_code;
            ajaxCall(srvr_request,queryRespHdlr);
        }

        function deleteForm() {
            var centre_code=document.getElementById("centre_code").value;
            srvr_request="querycentre.php?centre_code="+centre_code+
                        "&action=delete";
            ajaxCall(srvr_request,queryRespHdlr);
        }

        function displayTable() {
            srvr_request="querycentre.php";
            ajaxCall(srvr_request,dispTableHdlr);
        }

        function checkLoginCookie() {
            session_auth=getCookie('session_auth');
            logged_username=getCookie('username');

            if(session_auth!='kteam_auth' || !logged_username) {
                setCookie('redirector','centre.html',60*60*1000);
                window.location = "login.html";
            }
            initProducerSelect();
        }

    </script>

  </head>
  <body onLoad="checkLoginCookie()">

      <table border="1">
          <tr><td width="150"><a href="producer.html"><b>Producers</b></a></td>
              <td width="150"><a href="film.html"><b>Films</b></a></td>
              <td width="150"><a href="centre.html"><b>Centres</b></a></td>
              <td width="150"><a href="agent.html"><b>Agents</b></a></td>
              <td width="150"><a href="customer.html"><b>Tickets</b></a></td>
              <td width="150"><a href="ticket.html"><b>Ticket rate information</b></a></td>
              <td width="150"><a href="ticketstatus.html"><b>Status of ticket sales for each film</b></a></td>
              <td width="150"><a href="customerstatus.html"><b>Status for each customer</b></a></td>
              <td width="150"><a href="logout.php"><b>Logout</b></a></td></tr>
      </table>

        <table border="0">

            <tr><td><label>Centre Name&nbsp;*</label></td>
                <td><input id="centre_name" type=text size='20' value=''></td>
                <td><div id="centre_name_hint"> </div></td></tr>

            <tr><td><label>Centre Code&nbsp;*</label></td>
                <td><input id="centre_code" type=text size='20' value=''></td>
                <td><div id="centre_code_hint"> </div></td></tr>

            <tr><td><label>APLH. Code&nbsp;*</label></td>
                <td><input id="aplh_code" type=text size='20' value=''></td>
                <td><div id="aplh_code_hint"> </div></td></tr>

            <tr /> <tr /> <tr />
        </table>
        <table>
            <tr><td><input type="submit" onClick="saveForm()" value="Save" /></td>
                <td><input type="submit" onClick="resetForm()" value="Reset" /></td>
                <td><input type="submit" onClick="queryForm()" value="Query" /></td>
                <td><input type="submit" onClick="deleteForm()" value="Delete" /></td></tr>

        </table>
        <div id="centre_table">
            <button onclick="displayTable()">Display the centre table</button>
        </div>

  </body>
</html>
